<?php
/**
 * queen文件
 * ==============================================
 * 版权所有
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ==============================================
 * @date: 2016-12-18
 * @author: yqh
 * @version:
 */
namespace Home\Controller;
use Think\Controller;
class AdminController extends Controller {
	public function __construct() {
		parent::__construct();
		if (!$_SESSION['admin_Info']) {
			header("Location:./index.php?s=/Home/Check/login&from=admin");
		}
	}
	
	public function index(){
		$this->assign('meta_title',"在线测试系统");
		$this->display();
	}

	/**
	 * 函数用途描述：试卷列表
	 * @date: 2016-12-20上午10:34:05
	 * @author: yqh
	 * @return:
	 */
	public function paperList() {
		$post = I("post.");
		try {
			$paper = M('paper','ex_');
			$list = $paper->where(['status'=>1])->select();
			$this->assign('list',$list);
			$this->display();
		} catch (\Exception $e) {
			throw $e;
		}
	}
	
	/**
	 * 函数用途描述: 试卷设置
	 * @date: 2016-12-20上午10:35:00
	 * @author: yqh
	 * @return:
	 */
	public function paperSet() {
		$id = I("get.id");
		if ($id) {
			$paper = M('paper','ex_');
			$list = $paper->where(['id' => $id,'status' => 1])->find();
			if ($list) {
				$this->assign('list',$list);
			}
		}
		$this->display();
	}
	
	/**
	 * 函数用途描述: 编辑问卷
	 * @date: 2016-12-20上午10:35:25
	 * @author: yqh
	 * @return:
	 */
	public function paperEdit() {
		$id = I("get.id");
		if ($id) {
			try {
				$paper = M("paper");
				$sub = M("subject");
				$field = M("field");
				//试卷信息
				$pinfo = $paper->where(['id' => $id,'status' => 1])->field('id,name,description')->find();
				$result = array();
				if (count($pinfo) > 0) {
					$result['paper_info'] = $pinfo;
					//试卷的题目信息
					$subinfo = $sub->where(['paper_id' => $id,'status' => 1])->select();
					if (count($subinfo) > 0) {
						foreach ($subinfo as $k => $v) {
							$con['paper_id'] = $id;
							$con['subject_id'] = $v['id'];
							$con['status'] = 1;
							$finfo = $field->where($con)->field('id,paper_id,subject_id,title,correct')->select();
							$subinfo[$k]['field'] = $finfo;
						}
						$result['subject_info'] = $subinfo;
					}
				}
			} catch (\Exception $e) {
				throw $e;
			}
		}
		$this->assign("info",$result);
		$this->display();
	}
	
	public function savePaper() {
		$data = I("post.");
		$list = $data['data'];
		try {
			$paper = M("paper");
			$sub = M("subject");
			$field = M("field");
			$error = '';
			$p_array = array('name' => $data['paper_title'],
					'description' => $data['description']
			);
			$paper_id = $this->changeModel($data['paper_id'], $p_array, 'paper', $error);
			$paper->startTrans();
			foreach ($list as $key => $value) {
				if ($value['title_id']) { //题目更新
					$sub_array = array('title' => $value['title'],
							'score' => $value['score']
					);
				} else { //题目新增
					$sub_array = array('paper_id' => $paper_id,
							'title' => $value['title'],
							'type' => $value['type'],
							'score' => $value['score']
					);
					// 		        	$sub_id = $sub->add($sub_array);
				}
				$sub_id = $this->changeModel($value['title_id'], $sub_array, 'subject', $error);
				foreach ($value['field'] as $k => $v) {
					if ($v['field_id']) {
						$f_array = array('title' => $v['content'],
								'correct' => $v['correct']
						);
					} else {
						$f_array = array('paper_id' => $paper_id,
								'subject_id' => $sub_id,
								'title' => $v['content'],
								'correct' => $v['correct']
						);
					}
					$f_id = $this->changeModel($v['field_id'], $f_array, 'field', $error);
					// 	        	    $f_id = $field->add($f_array);
				}
			}
			$paper->commit();
			$this->ajaxReturn(1);
		} catch (\Exception $e) {
			$paper->rollback();
			throw $e;
		}
	}
	
	/**
	 * 函数用途描述: 对数据表的更新和新增操作
	 * @date: 2016-12-21上午11:23:22
	 * @author: yqh
	 * @param: int $id 数据id
	 * @param: array $data 更新的数据
	 * @param:string $table 表名
	 * @return:boolean
	 */
	public function changeModel($id,$data,$table,&$error) {
		$obj = M($table);
		try {
			if ($id) { //更新操作
				$data['updated_time'] = date("Y-m-d H:i:s",time());
				$result = $obj->where(['id' => $id])->save($data);
				$lastId = $result ? $id : false;
			} else { //新增
				$data['updated_time'] = date("Y-m-d H:i:s",time());
				$data['created_time'] = date("Y-m-d H:i:s",time());
				$lastId = $obj->add($data);
			}
			return $lastId;
		} catch (\Exception $e) {
			$error = $e->getMessage();
			return false;
		}
		 
	}
	
	public function paperDel() {
		$id = I("post.id");
		if (!$id) {
			$this->ajaxReturn(0);
		}
		$data['status'] = 0;
		$result = $this->changeModel($id, $data, 'paper');
		$this->ajaxReturn($result);
	}
}